import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
import homeChildrens from './homechildrens'
const routes: Array<RouteRecordRaw> = [
  { //beforeEach
    path: "/",
    redirect: {name: "spsrcmain"}
  },
  {
    path:'/404NotFound',
    name:'404',
    component:() => import('@/pages/404/index.vue'),
    meta:{
      title:'页面丢失了'
    },
  },
  {
    path:'/login',
    name:'login',
    component: () => import(/* webpackChunkName: "login" */ '@/pages/login/index.vue'),
    meta:{
      title:'登录'
    }
  },
  {
    path: "/spsrcmain",
    name: "spsrcmain",
    redirect: {name: "homepage"},
    component: () => import(/* webpackChunkName: "Home" */ '@/pages/home/index.vue'),
    meta:{
      title:'主页'
    },
    children:homeChildrens
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

import store from '../store'
router.beforeEach((to,from,next)=>{
  console.log({to,from})
  const { token } = store.getters
  const { name:toname } = to
  if(!token && toname!='login') next('/login')
  const { name:fromname } = from
  // if (toname == null)
  //   next(`/404NotFound`)
  console.log({token},fromname,"=>",toname)
  next()
})

router.afterEach((to)=>{
  let { meta:{ title }}= to
  title = `StartRiverCup${title? `-${title}`:''}`
  document.title = title
})


export default router;
